package com.xiyu.service.repository.elder.register;

import com.xiyu.service.model.elder.info.ElderInfoFetcher;
import com.xiyu.service.model.elder.register.ElderAccidentFetcher;
import com.xiyu.service.vo.elder.accident.AccidentPageQueryInput;
import com.xiyu.service.model.elder.register.ElderAccident;
import com.xiyu.service.model.elder.register.ElderAccidentTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;

import java.util.Optional;

public interface ElderAccidentRepository extends JRepository<ElderAccident, Long> {
    ElderAccidentTable elderAccidentTable = ElderAccidentTable.$;

    default Page<ElderAccident> pageQuery(AccidentPageQueryInput inputVO){
        return sql().createQuery(elderAccidentTable)
                .orderBy(elderAccidentTable.id().desc())
                .select(elderAccidentTable.fetch(ElderAccidentFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    default Optional<ElderAccident> findDetailById(Long id){
        return sql().createQuery(elderAccidentTable)
                .where(elderAccidentTable.id().eq(id))
                .select(elderAccidentTable.fetch(ElderAccidentFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchOptional();
    }

}